package com.itheima.appserver.controller;

import cn.hutool.core.convert.Convert;
import com.itheima.appserver.interceptor.UserThreadLocal;
import com.itheima.appserver.service.MyCenterService;
import com.itheima.appserver.service.UserInfoService;
import com.itheima.appserver.service.UserService;
import com.itheima.commons.vo.reception.CountsVo;
import com.itheima.commons.vo.reception.PageResult;
import com.itheima.commons.vo.reception.SettingsVo;
import com.itheima.commons.vo.reception.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @author yaohu
 * @date 2021/12/12
 * @Version 1.0
 * @describe
 */
@RequestMapping("users")
@RestController
public class MyCenterController {

    @Autowired
    private MyCenterService myCenterService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 根据用户id查询用户信息
     *
     * @param userId 用户id，如果为空，表示查询当前登录人的信息
     * @return
     */
    @GetMapping
    public ResponseEntity queryUserInfoByUserId(@RequestParam(value = "userID", required = false) Long userId) {
        UserInfoVo userInfoVo = myCenterService.queryUserInfoByUserId(userId);
        return ResponseEntity.ok(userInfoVo);
    }

    /**
     * 互相喜欢，喜欢，粉丝 - 统计
     *
     * @return
     */
    @GetMapping("counts")
    public ResponseEntity queryCounts() {
        CountsVo countsVo = myCenterService.queryCounts();
        return ResponseEntity.ok(countsVo);
    }

    /**
     * 互相关注、我关注、粉丝、谁看过我 - 翻页列表
     *
     * @param type     1 互相关注 2 我喜欢 3 粉丝 4 谁看过我
     * @param page
     * @param pageSize
     * @param nickname
     * @return
     */
    @GetMapping("friends/{type}")
    public ResponseEntity queryLikeList(@PathVariable("type") String type,
                                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                                        @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                        @RequestParam(value = "nickname", required = false) String nickname) {
        page = 1;
        PageResult pageResult = myCenterService.queryLikeList(Integer.valueOf(type), page, pageSize, nickname);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 取消喜欢
     *
     * @param userId
     * @return
     */
    @DeleteMapping("like/{uid}")
    public ResponseEntity disLike(@PathVariable("uid") Long userId) {
        myCenterService.disLike(userId);
        return ResponseEntity.ok(null);
    }

    /**
     * 关注粉丝
     *
     * @param userId
     * @return
     */
    @PostMapping("fans/{uid}")
    public ResponseEntity likeFan(@PathVariable("uid") Long userId) {
        myCenterService.likeFan(userId);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改用户信息
     *
     * @param param
     * @return
     */
    @PutMapping
    public ResponseEntity updateInfo(@RequestBody Map<String, Object> param) {
        // 昵称
        String nickname = Convert.toStr(param.get("nickname"));
        // 生日 1990-01-01
        String birthday = Convert.toStr(param.get("birthday"));
        // 性别 man woman
        String gender = Convert.toStr(param.get("gender"));
        // 城市
        String city = Convert.toStr(param.get("city"));
        // 学历
        String education = Convert.toStr(param.get("education"));
        // 月收入
        String income = Convert.toStr(param.get("income"));
        // 行业
        String profession = Convert.toStr(param.get("profession"));
        // 婚姻状态（0未婚，1已婚）
        Integer marriage = Convert.toInt(param.get("marriage"));
        myCenterService.updateInfo(nickname, birthday, gender, city, education, income, profession, marriage);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改用户头像
     *
     * @param headPhoto
     * @return
     */
    @PostMapping("/header")
    public ResponseEntity updateLogo(@RequestParam("headPhoto") MultipartFile headPhoto) {
        // 调用userInfoService中的上传头像方法，会覆盖之前的地址
        userInfoService.loginReginfoHead(headPhoto);
        return ResponseEntity.ok(null);
    }

    /**
     * 用户通用设置 - 读取
     *
     * @return
     */
    @GetMapping("/settings")
    public ResponseEntity findSettings() {
        SettingsVo settingsVo = myCenterService.findSettings();
        return ResponseEntity.ok(settingsVo);
    }

    /**
     * 通知设置 - 保存
     *
     * @param param
     * @return
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity addSettings(@RequestBody Map<String, Boolean> param) {
        // 是否推送喜欢通知
        Boolean likeNotification = param.get("likeNotification");
        // 是否推送评论通知
        Boolean pinglunNotification = param.get("pinglunNotification");
        // 是否推送公告通知
        Boolean gonggaoNotification = param.get("gonggaoNotification");
        myCenterService.addSettings(likeNotification, pinglunNotification, gonggaoNotification);
        return ResponseEntity.ok(null);
    }

    /**
     * 设置陌生人问题
     *
     * @param param
     */
    @PostMapping("/questions")
    public ResponseEntity updateQuestion(@RequestBody Map<String, String> param) {
        String content = param.get("content");
        myCenterService.updateQuestion(content);
        return ResponseEntity.ok(null);
    }

    /**
     * 分页查询黑名单列表
     *
     * @param page
     * @param pagesize
     * @return
     */
    @PostMapping("/blacklist")
    public ResponseEntity queryBlackList(@RequestParam(value = "page", required = true) Integer page,
                                         @RequestParam(value = "pagesize", required = true) Integer pagesize) {
        PageResult pageResult = myCenterService.queryBlackList(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 黑名单 - 移除
     *
     * @param userId
     * @return
     */
    @DeleteMapping("/blacklist/:uid")
    public ResponseEntity removeBlackList(@PathVariable(value = "uid") Long userId) {
        myCenterService.removeBlackList(userId);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号- 1 发送短信验证码
     *
     * @return
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {
        userService.sendLoginMsg(UserThreadLocal.getUserMobile());
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号 - 2 校验验证码
     *
     * @param param
     * @return
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map<String, String> param) {
        String verificationCode = param.get("verificationCode");
        Boolean verification = myCenterService.checkVerificationCode(verificationCode);
        return ResponseEntity.ok(verification);
    }

    /**
     * 修改手机号 - 3 保存
     *
     * @param param
     * @return
     */
    @PostMapping("/phone/phone")
    public ResponseEntity savePhone(@RequestBody Map<String, String> param) {
        String phone = param.get("phone");
        myCenterService.savePhone(phone);
        return ResponseEntity.ok(null);
    }
}
